//
//  LTThemeController.m
//  LocalTravelIPhone
//
//  Created by zheng ji on 14-3-28.
//  Copyright (c) 2014年 jxw. All rights reserved.
//

#import "LTThemeController.h"

@interface LTThemeController ()
{
    NSArray *_themeArray;
    UIStoryboard *_storyBoard;
    GADBannerView* bannerView_;
    MobiSageAdBanner * adBanner;
}
@end

@implementation LTThemeController
- (UIViewController *)viewControllerToPresent
{
    return self;
}
-(void)addSageAd{
    adBanner = [[MobiSageAdBanner alloc] initWithAdSize:Ad_320X50 withDelegate:self];
    adBanner.frame = CGRectMake(0, 0, 320, 50);
    [adBanner setInterval:Ad_Refresh_30];
    //设置广告轮播动画效果
    [adBanner setSwitchAnimeType:Random];
    
    [self.collectionView addSubview:adBanner];
}

-(void)addGoogleAdv{
    bannerView_ = [[GADBannerView alloc]
                   initWithFrame:CGRectMake(0.0,
                                            0.0 ,
                                            GAD_SIZE_320x50.width,
                                            GAD_SIZE_320x50.height)];    // 指定广告单元ID。
    
    bannerView_.adUnitID = [CommonConfigUtil getGoogleAdvId];
    
    bannerView_.rootViewController = self;
    [self.collectionView addSubview:bannerView_];
    GADRequest *request = [GADRequest request];
    // request.testDevices = @[ GAD_SIMULATOR_ID ];
    [bannerView_ loadRequest:request];
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    if(adBanner==nil){
        [self addSageAd];
    }
    
    /*
     if(bannerView_ ==nil){
     [self addGoogleAdv];
     }*/
    
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.navigationItem.title=@"选择主题";
    
   [self.collectionView registerNib:[UINib nibWithNibName:@"ThemeCollectionCell" bundle:nil] forCellWithReuseIdentifier:@"ThemeCollectionCell"];
    
    self.view.backgroundColor=BACKGROUND_COLOR;
    self.collectionView.backgroundColor=[UIColor clearColor];
    
    _themeArray=[NSArray arrayWithObjects:[LTTheme initWith:@"" name:@"全部"],
                [LTTheme initWith:@"21" name:@"赏花"],
                [LTTheme initWith:@"22" name:@"水上乐园"],
                [LTTheme initWith:@"20" name:@"海洋馆"],
                [LTTheme initWith:@"4" name:@"漂流避暑"],
                [LTTheme initWith:@"3" name:@"游船"],
                [LTTheme initWith:@"9" name:@"农家乐园"],
                [LTTheme initWith:@"11" name:@"登山探险"],
                [LTTheme initWith:@"14" name:@"动植物园"],
                [LTTheme initWith:@"12" name:@"山水园林"],
                [LTTheme initWith:@"5" name:@"主题乐园"],
                [LTTheme initWith:@"6" name:@"古镇山水"],
                [LTTheme initWith:@"13" name:@"海岛度假"],
                [LTTheme initWith:@"8" name:@"户外拓展"],
                [LTTheme initWith:@"10" name:@"名胜古迹"],
                [LTTheme initWith:@"16" name:@"影视基地"],
                [LTTheme initWith:@"17" name:@"城市观光"],
                [LTTheme initWith:@"15" name:@"博物馆"],
                [LTTheme initWith:@"7" name:@"演出表演"],
                [LTTheme initWith:@"1" name:@"温泉"],
                [LTTheme initWith:@"19" name:@"文化宗教"],
                [LTTheme initWith:@"2031408" name:@"采摘"],
                [LTTheme initWith:@"2" name:@"滑雪"],
                nil];
    
  

}
- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    if(_themeArray !=nil) return _themeArray.count;
    
    return 0;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
	return CGSizeMake(80,30);
}
-(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    ThemeCollectionCell* cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"ThemeCollectionCell" forIndexPath:indexPath];
    if(_themeArray !=nil && _themeArray.count>indexPath.row){
    
        LTTheme* theme=[_themeArray objectAtIndex:indexPath.row];
        
        cell.lblThemeName.text=theme.themeName;
       // cell.layer
    }
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if(_themeArray ==nil || _themeArray.count<indexPath.row) return ;
    
    
    if(_storyBoard==nil){
       _storyBoard=[UIStoryboard storyboardWithName:@"Main" bundle:nil];
    }
    
    LTSearchSceneryController* ctrl=[_storyBoard instantiateViewControllerWithIdentifier:@"LTSearchSceneryController"];
    ctrl.theme=[_themeArray objectAtIndex:indexPath.row];
    [self.navigationController pushViewController:ctrl animated:YES];
    ctrl=nil;
    //[self presentViewController:searchController animated:YES completion:nil];
    
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(60, 15, 15, 15);
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)dealloc{
    adBanner=nil;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
